﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.Data.Odbc;
using System.Collections.Specialized;

namespace EuroAudit
{
    public partial class SessionPageBase : System.Web.UI.Page
    {
        public DataSet GetData(String queryString, String CNStringName)
        {
            string s1 = "";
            string s2 = "";
            s1 += "e";
            s2 += "E";
            s1 += "a";
            s2 += "A";
            s1 += "u";
            s2 += "!";
            s1 += "s";
            s2 += "2";
            s1 += "e";
            s2 += "i";
            s1 += "r";
            s2 += "n";

            // Retrieve the connection string stored in the Web.config file.
            string ProvName = ConfigurationManager.ConnectionStrings[CNStringName].ProviderName;
            String connectionString = ConfigurationManager.ConnectionStrings[CNStringName].ConnectionString;
            connectionString = string.Format(connectionString, s1, s2);

            DataSet ds = new DataSet();

            try
            {
                // Connect to the database and run the query.
                if (ProvName == "MySql.Data")
                {
                    MySqlConnection connection = new MySqlConnection(connectionString);
                    // Connect to the database and run the query.
                    //connection.ConnectionTimeout = 100;
                    connection.Open();
                    MySqlDataAdapter adapter = new MySqlDataAdapter(queryString, connection);
                    adapter.SelectCommand.CommandTimeout = 100;
                    // Fill the DataSet.
                    adapter.Fill(ds);
                    /*if (GetLastInsertID == true)
                    {
                        queryString = "SELECT LAST_INSERT_ID() as LID";
                        adapter = new MySqlDataAdapter(queryString, connection);
                        adapter.Fill(ds, "tLID");
                    }*/
                    connection.Close();
                }
                if (ProvName == "System.Data.Odbc")
                {
                    OdbcConnection connection = new OdbcConnection(connectionString);
                    // Connect to the database and run the query.
                    connection.ConnectionTimeout = 100;
                    connection.Open();
                    OdbcDataAdapter adapter = new OdbcDataAdapter(queryString, connection);
                    adapter.SelectCommand.CommandTimeout = 100;
                    // Fill the DataSet.
                    adapter.Fill(ds);
                    /*if (GetLastInsertID == true)
                    {
                        queryString = "SELECT LAST_INSERT_ID() as LID";
                        adapter = new OdbcDataAdapter(queryString, connection);
                        adapter.Fill(ds, "tLID");
                    }*/
                    connection.Close();
                }

            }
            catch (Exception ex)
            {

                // The connection failed. Display an error message.
                //Message.Text = "Unable to connect to the database.";


            }

            return ds;

        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserName"].ToString() == "" || Session["UserName"].ToString() == "guest")
            {

                Response.Write("User not logged in!");
                Response.End();
            }
            else
            {
                Response.Write("User " + Session["UserName"].ToString() + " logged in.");
            }

        }
    }
}
